


//选择按日期和月份筛选
$('.choose-type span').on('click',function(){
  $(this).addClass('active').siblings().removeClass('active');
   var attr = $(this).attr('class');
   console.log($(this).hasClass('choose-month'))
   if($(this).hasClass('choose-month')){
     $('.datepicker-month').show();
     $('.datepicker-date').hide();
		 getList(2,$('#endMonth').val()+'-01',function(res){
		 	 echartDeal(option,res);
			  $("#tableFromData").bootstrapTable('load', res);
		 	 if (option && typeof option === "object") {
		 	     myChart.setOption(option, true);
		 	 }
		 });
   }else{
     $('.datepicker-month').hide();
     $('.datepicker-date').show();
		 getList(1,$('#endDate').val(),function(res){
		 	 echartDeal(option,res);
			  $("#tableFromData").bootstrapTable('load', res);
		 	 if (option && typeof option === "object") {
		 	     myChart.setOption(option, true);
		 	 }
		 });
   }
})

  // 	自定义格式
    $.fn.datetimepicker.dates['zdy'] = {
      days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
      daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
      daysMin:  ["日", "一", "二", "三", "四", "五", "六"],
      months: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
      monthsShort: ["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
      today: "今天",
      format:"yyyy-mm-dd",
      titleFormat:"yyyy-mm-",
      weekStart:1,
      suffix: [],
      meridiem: ["上午", "下午"]
    };
    var startDate = sevenDaysDate(formatDate().endDate,'before');
    var endDate = formatDate().endDate;
    $('#startDate').val(startDate);//开始日期--今日前七日
    $('#endDate').val(endDate);//结束日期--最大为今日
    $('#startDate').datetimepicker({
      format:'yyyy-mm-dd',
      language:  'zdy',
      weekStart: 1,
      todayBtn:  1,
      autoclose: 1,
      endDate:startDate,
      minView:2,
      maxView:3
    }).on('changeDate', function(ev){
      var chooseStartDate=$("#startDate").val();//选择了新的开始时间--结束时间倒退七天
      var changeEndDate = sevenDaysDate(chooseStartDate,'after');;
      $('#endDate').val(changeEndDate);
    });

$('#endDate').datetimepicker({
  format:'yyyy-mm-dd',
  language:  'zdy',
  weekStart: 1,
  todayBtn:  1,
  autoclose: 1,
  endDate:endDate,
  minView:2,
  maxView:3
}).on('changeDate',function(){
  var chooseEndDate=$("#endDate").val();//选择了新的结束时间--结束时间前推七天
  var changeStartDate = sevenDaysDate(chooseEndDate,'before');;
  $('#startDate').val(changeStartDate);
})

//开始月份--结束月份
// var startMonth = ;
var endMonth = formatDate().endMonth;
var startMonth = sevenMonth(endMonth,"before");
$('#endMonth').val(endMonth);
$('#startMonth').val(startMonth);

$('#startMonth').datetimepicker({
    format: 'yyyy-mm',
    weekStart: 1,
    autoclose: true,
    startView: 3,
    minView: 3,
    language: 'zdy',
    endDate:startMonth
}).on('changeDate',function(){
  var chooseStartMonth=$("#startMonth").val();//选择了新的开始时间--结束时间倒退七天
  var changeEndMonth = sevenMonth(chooseStartMonth,'after');;
  $('#endMonth').val(changeEndMonth);
})

$('#endMonth').datetimepicker({
    format: 'yyyy-mm',
    weekStart: 1,
    autoclose: true,
    startView: 3,
    minView: 3,
    language: 'zdy',
    endDate:endMonth
}).on('changeDate',function(){
  var chooseEndMonth=$("#endMonth").val();//选择了新的结束时间--结束时间前推七天
  var changeStartMonth = sevenMonth(chooseEndMonth,'before');;
  $('#startMonth').val(changeStartMonth);
})


//统计图
var dom = document.getElementById("echart-container");
var myChart = echarts.init(dom);
var app = {};
var option = null;
option = {
    title: {
        text: ''
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
  
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },

    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: []
    },
    yAxis: {
        type: 'value'
    },
    series: [

    ]
};
myChart.setOption(option, true);

var keyObj = {
	'regCountTotal':{name:'注册总人数'},
	'chargeRoomCards':{name:'充值总房卡数'},
	'chargeGoldCoins':{name:'充值总金币数'},
	'money':{name:'充值总金额'},
	'consumeRoomCards':{name:'售出房卡数'},
	'playCount':{name:'游戏总局数'},
	'newRegCount':{name:'新增注册人数'},
	'loginCount':{name:'日活动人数'}
}


//初始获取的是今天以及今天之前七天的
getList(1,endDate,function(res){
	 echartDeal(option,res);
	 if (option && typeof option === "object") {
	     myChart.setOption(option, true);
	 }
});

//点击搜索
$('.search-bt').on('click',function(){
	 //获取type
	 var type = $('.choose-type span.active').data('type');
	 type = type=='day'?1:2;
	 var date = type==1?$('#endDate').val():$('#endMonth').val()+'-01';
	 getList(type,date,function(res){
		  $("#tableFromData").bootstrapTable('load', res);
			 echartDeal(option,res);
			 myChart.setOption(option, true);
	 })
})

// echartDeal(list);
//统计图的处理
function echartDeal(option,list){
	option.legend.data = ['注册总人数','充值总房卡数','充值总金币数','充值总金额','售出房卡数','游戏总局数','新增注册人数','日活动人数'];
	option.legend.selected = {
		'充值总房卡数':false,
		'充值总金币数':false,
		'充值总金额':false,
		'售出房卡数':false,
		'游戏总局数':false,
		'新增注册人数':false,
		'日活动人数':false
	};
	option.series = [];
	   var dateArr = [];
		 var name = '';
		 for(var i in keyObj){
			  var data = [];
				for(var j in list){
					data.push(list[j][i]);
				}
				keyObj[i].data = data;
				 var obj = {
					 name:keyObj[i].name,
					 type:'line',
					 stack: '总量',
					 data:data
				 };
				 option.series.push(obj);
		 }
		 for(var i in list){
			 dateArr.push(list[i].createTime.split(" ")[0]);
		 }
	   option.xAxis.data = dateArr;
}

//获取列表
function getList(type,date,cb){
	 var params = {
		 type:type,//1日 2月
		 date:date,
		 pageNum:1,
		 pageSize:10,
		 typePosition:1//1为超管   2为代理
	 }
	_POST('/doulai/playCount/list',params).then(function(res){
		 InitTable(res.rows)
		 cb&&cb(res.rows)
	})
}

//初始化列表
function InitTable(data){
  $('#tableFromData').bootstrapTable({
        data: data
  })
}

//格式化时间
function formatDate(date){
   if(!date){
			 var date = new Date();
			 var year = date.getFullYear();
			 var month = date.getMonth()*1+1+'';
			 var maxMonth = month.length==1?'0'+(month*1+1):month*1+1;
			 month = month.length==1?'0'+month:month;
			 var day = date.getDate()+'';
			 day = day.length==1?'0'+day:day;
			 var startDate = year+'-'+month+'-01';
			 var endDate = year+'-'+month+'-'+day;
			 var endMonth =  year+'-'+maxMonth;
			 return {startDate:startDate,endDate:endDate,endMonth:endMonth};
   }else{
        var year = date.getFullYear();
        var month = (date.getMonth()+1);
        month=month<=9?'0'+month:month;
        var day = date.getDate();
        day=day<=9?'0'+day:day; 
        var datetime = year+'-'+month+'-'+day;
        return datetime;
   }
}


//7天时间差
function sevenDaysDate(dateTime,type){/*  *///before是前七日 after是后七日
      var time = new Date(dateTime).getTime();
      var date=new Date(time+(type=='before'?"-6":"+6") * 24 * 3600 * 1000);
      date = formatDate(date);
      return date;
}

//7月时间差
function sevenMonth(dateMonth,type){
  var year = dateMonth.split('-')[0]*1;
  var month = dateMonth.split('-')[1]*1;
  if(type=="after"){//后7月
    var afterMonth = month+6;
    if(afterMonth>12){//加1年
      year = year+1;
      afterMonth = afterMonth - 12;
    }
    afterMonth = afterMonth<10?'0'+afterMonth:afterMonth;
  }else{//前7月
    var beforeMonth = month-6;
    if(beforeMonth<0||beforeMonth==0){//加1年
      year = year-1;
    }
    beforeMonth = beforeMonth==0?12:12-Math.abs(beforeMonth);
    beforeMonth = beforeMonth<10?'0'+beforeMonth:beforeMonth;
  }
  return dateMonth = year+'-'+(type=="after"?afterMonth:beforeMonth);
}

window.onresize = myChart.resize; 